home *** CD-ROM | disk | FTP | other *** search
/ Giga Games 1 / Giga Games.iso / net / usenet / volume1 / robots2 < prev    next >
Encoding:
Internet Message Format  |  1987-07-08  |  36.2 KB

  1. Path: uunet!seismo!husc6!mit-eddie!uw-beaver!tektronix!tekgen!tekred!games-request
  2. From: games-request@tekred.TEK.COM
  3. Newsgroups: comp.sources.games
  4. Subject: v01i080:  robots2 - Game of robots, version 2
  5. Message-ID: <1380@tekred.TEK.COM>
  6. Date: 8 Jul 87 21:47:06 GMT
  7. Sender: billr@tekred.TEK.COM
  8. Lines: 1625
  9. Approved: billr@tekred.TEK.COM
  10.  
  11. Submitted by: Julian Onions <jpo@cs.nott.ac.uk>
  12. Comp.sources.games: Volume 1, Issue 80
  13. Archive-name: robots2
  14.  
  15.     [Compiled and ran OK on our 4.3bsdBeta Vax.   -br]
  16.  
  17. [[An upgraded version of the robots game. Should work on sys5 as well as
  18. 4.2/3 although not test rigorously. The README/manual page
  19. describes most of the interesting stuff.
  20. I can be reached as either
  21.     jpo@cs.nott.ac.uk
  22.     jpo%cs.nott.ac.uk@ucl-cs.arpa
  23.     ...!seismo!mcvax!ukc!nott-cs!jpo - least preferable.
  24.  
  25. Julian]]
  26.  
  27. -----------------------
  28. #! /bin/sh
  29. # This is a shell archive.  Remove anything before this line, then unpack
  30. # it by saving it into a file and typing "sh file".  To overwrite existing
  31. # files, type "sh file -c".  You can also feed this as standard input via
  32. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  33. # will see the following message at the end:
  34. #        "End of shell archive."
  35. # Contents:  READ_ME Makefile good.c main.c makelog opt.c robot.c
  36. #   robots.6 robots.h score.c user.c
  37. # Wrapped by billr@tekred on Wed Jul  8 14:40:28 1987
  38. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  39. if test -f READ_ME -a "${1}" != "-c" ; then 
  40.   echo shar: Will not over-write existing file \"READ_ME\"
  41. else
  42. echo shar: Extracting \"READ_ME\" \(1371 characters\)
  43. sed "s/^X//" >READ_ME <<'END_OF_READ_ME'
  44. XRobots - version 2
  45. X
  46. XThis version has several features over the original version but is
  47. Xstill very much in the same spirit. Amongst the new features are
  48. X
  49. X- Fast robots, these are robots that move at twice the speed of
  50. X    normal robots.
  51. X- Option settings in the style of rogue.
  52. X- movable scrap heaps.
  53. X- random & safe teleports with the ability to choose.
  54. X- antimatter
  55. X
  56. XTo build.
  57. XIt should build on most sane systems,
  58. XYou may want to play with some of the variables in Makefile, there
  59. Xare a couple of version specific things to look for.
  60. XIf you are on a BSD4.2 or better system add -DBSD42 to the CFLAGS
  61. Xin the Makefile.
  62. XSystem 5 may need to remove -ltermcap from the LIBS line in Makefile.
  63. XAlso if BSD42 is not defined, the score directory needs to be
  64. Xwritable to lock the files.
  65. XYou may want to play with the location of the high score files which
  66. Xare #defined near the begining of robots.h.
  67. X
  68. XType make, make install, and enjoy.
  69. X
  70. XBugs, comments, flames, offers of cash, aston martins to those below
  71. X    Julian Onions <jpo@cs.nott.ac.uk>
  72. X    Graeme Lunt <gal@cs.nott.ac.uk>
  73. X    Allan Black <allan@cs.strath.ac.uk>
  74. X
  75. XStop Press:
  76. X    Also available from jpo@cs.nott.ac.uk, a version of robomatic
  77. X    which does a reasonable job of playing the game of robots.
  78. X    Current high scores are -
  79. X
  80. X   1 482538104   Graeme (gal): eaten on level 85.
  81. X   6   8476863   Robomatic II (jpo): eaten on level 23.
  82. END_OF_READ_ME
  83. if test 1371 -ne `wc -c <READ_ME`; then
  84.     echo shar: \"READ_ME\" unpacked with wrong size!
  85. fi
  86. # end of overwriting check
  87. fi
  88. if test -f Makefile -a "${1}" != "-c" ; then 
  89.   echo shar: Will not over-write existing file \"Makefile\"
  90. else
  91. echo shar: Extracting \"Makefile\" \(979 characters\)
  92. sed "s/^X//" >Makefile <<'END_OF_Makefile'
  93. X# just -lcurses for SYS V
  94. XLIBS    = -lcurses -ltermcap
  95. X# add -DBSD42 for performance wins on 4.2 or better
  96. XIFDEF    = -DBSD42
  97. XCFLAGS  = $(IFDEF) #-g
  98. XLINT    = lint
  99. XLLFLAGS = -haxbc
  100. XSHAR    = shar
  101. XSHFLGS  = -v -c
  102. XHEADERS = robots.h
  103. XSRC    = good.c main.c opt.c robot.c score.c user.c
  104. XOBJS    = good.o main.o opt.o robot.o score.o user.o
  105. XMAN    = robots.6
  106. XMISC    = READ_ME Makefile
  107. XDIST    = $(MISC) $(SRC) $(MAN) $(HEADERS)
  108. XMANUAL  = /usr/man/man6/robots.6    # where the manual page goes
  109. XBINARY  = /usr/sheriff/jpo/games            # Where the binary goes
  110. X
  111. Xrobots: ${OBJS}
  112. X    $(CC) $(CFLAGS) -o $@ ${OBJS} $(LIBS)
  113. X
  114. Xinstall: robots robots.6
  115. X    install -s robots $(BINARY)    # or strip robots && mv robots $(BINARY)
  116. X#    install -c robots.6 $(MANUAL)    # or cp robots.6 $(MANUAL)
  117. X
  118. Xtidy: clean
  119. Xclean:
  120. X    rm -f ${OBJS} core a.out lint.out shar.out
  121. X
  122. Xclobber: clean
  123. X    rm -f robots
  124. X
  125. Xlint: ${SRC} $(HEADERS)
  126. X    $(LINT) $(LLFLAGS) $(IFDEF) ${SRC} $(LIBS)
  127. X
  128. Xshar: shar.out
  129. Xshar.out: $(DIST)
  130. X    $(SHAR) $(SHFLGS) $(DIST) > $@
  131. X
  132. X${OBJS}: ${HEADERS}
  133. END_OF_Makefile
  134. if test 979 -ne `wc -c <Makefile`; then
  135.     echo shar: \"Makefile\" unpacked with wrong size!
  136. fi
  137. # end of overwriting check
  138. fi
  139. if test -f good.c -a "${1}" != "-c" ; then 
  140.   echo shar: Will not over-write existing file \"good.c\"
  141. else
  142. echo shar: Extracting \"good.c\" \(2907 characters\)
  143. sed "s/^X//" >good.c <<'END_OF_good.c'
  144. X# include "robots.h"
  145. X
  146. X/*
  147. X * good.c: Figure out all possible good moves.
  148. X * This piece of code use to be trivial - then fast robots came
  149. X * and moveable heaps and its now an AI expert system in its own right!
  150. X */
  151. X
  152. Xgood_moves ()
  153. X{
  154. X    register int test_x, test_y;
  155. X    register char *m, *a;
  156. X    static char moves[] = "hjklyubn.a";
  157. X    static char ans[sizeof (moves)];
  158. X    char savebuf[9], savechar;
  159. X    int i,j, notsilly;
  160. X
  161. X    a = ans;
  162. X    for(m = moves; *m != 'a'; m++) {
  163. X        test_x = my_x+xinc(*m);
  164. X        test_y = my_y+yinc(*m);
  165. X        move(test_y,test_x);
  166. X        switch(inch()) {
  167. X            case ' ':
  168. X            case DOT:
  169. X            case ME:
  170. X                if( isgood(test_y, test_x) )
  171. X                    *a++ = *m;
  172. X                break;
  173. X            case SCRAP:
  174. X                if (moveable_heaps) {
  175. X                int nscrap_x,nscrap_y;
  176. X                nscrap_x = test_x +xinc(*m);
  177. X                nscrap_y = test_y +yinc(*m);
  178. X                move(nscrap_y,nscrap_x);
  179. X                if (inch() == ' ') {
  180. X                    addch(SCRAP);
  181. X                    if (isgood(test_y,test_x))
  182. X                        *a++ = *m;
  183. X                    move(nscrap_y,nscrap_x);
  184. X                    addch(' ');
  185. X                    }
  186. X                }
  187. X                break;
  188. X        }
  189. X    }
  190. X    /* now lets do the antimatter case */
  191. X    if (free_teleports) {
  192. X    notsilly = 0;    /* silly to do it if not get robots */
  193. X    for(i= -1;i <= 1; i++)
  194. X        for(j= -1; j<= 1; j++) {
  195. X            move(my_y+i,my_x+j);
  196. X            savechar = inch();
  197. X            if (savechar == FROBOT || savechar == ROBOT) {
  198. X                savebuf[ (i+1)*3 +j +1] = savechar;
  199. X                addch(' ');
  200. X                notsilly = 1;
  201. X                }
  202. X            else
  203. X                savebuf[ (i+1)*3 +j +1] = ' ';
  204. X            }
  205. X    if( notsilly && isgood(my_y,my_x) )
  206. X        *a++ = *m;
  207. X
  208. X    for(i= -1;i <= 1; i++)
  209. X        for(j= -1; j<= 1; j++) {
  210. X            move(my_y+i,my_x+j);
  211. X            savechar = savebuf[ (i+1)*3 +j +1];
  212. X            if (savechar == FROBOT || savechar == ROBOT)
  213. X                addch(savechar);
  214. X            }
  215. X    }
  216. X    *a = 0;
  217. X    if(ans[0]) {
  218. X        a = ans;
  219. X    } else {
  220. X        a = "Forget it!";
  221. X    }
  222. X    mvprintw(LINES-1,MSGPOS,"%-*.*s",RVPOS-MSGPOS,RVPOS-MSGPOS,a);
  223. X}
  224. X
  225. Xisgood(ty,tx)
  226. Xregister int tx, ty;
  227. X{
  228. X    register int x, y;
  229. X
  230. X    for(x = -2; x <= 2; x++) {
  231. X        for(y = -2; y <= 2; y++) {
  232. X            if ( abs(x) > 1 || abs(y) > 1 ) { /* we are 2 steps out */
  233. X                if( blocked(ty, tx, y, x))
  234. X                    continue;
  235. X                move(ty+y,tx+x);
  236. X                if ( abs(x) == 2 && abs(y) == 2
  237. X                            && inch() == FROBOT){
  238. X                    return FALSE;    }
  239. X                if ( x == -1 && scan(ty+y, tx+x, 0, 1) )
  240. X                    return FALSE;
  241. X                if ( y == -1 && scan(ty+y, tx+x, 1, 0) )
  242. X                    return FALSE;
  243. X            } /* outer perimeter checked */
  244. X            else {
  245. X                move(ty+y,tx+x);
  246. X                switch(inch()) {
  247. X                    case FROBOT:
  248. X                    case ROBOT: /* robot next to you */
  249. X                        return FALSE;
  250. X                }
  251. X            }
  252. X        }
  253. X    }
  254. X    return TRUE;
  255. X}
  256. X
  257. Xscan(y, x, yi, xi) /* scan along this line looking for collision conditions */
  258. Xint x, y, xi, yi;
  259. X{
  260. X    int rcount = 0;
  261. X    register int ctr;
  262. X
  263. X    for(ctr = 0;ctr<=2;ctr++) {
  264. X        move(y + (ctr*yi), x + (ctr*xi));
  265. X        switch(inch()) {
  266. X            case FROBOT:
  267. X                rcount += 4;
  268. X                break;
  269. X            case ROBOT:
  270. X                rcount ++;
  271. X                break;
  272. X        }
  273. X    }
  274. X    return rcount == 4;
  275. X}
  276. X
  277. Xblocked(my, mx, y, x)
  278. Xregister int    my, mx, y, x;
  279. X{
  280. X    if ( x < 0 ) x++;
  281. X    if ( y < 0 ) y++;
  282. X    if ( x > 0 ) x--;
  283. X    if ( y > 0 ) y--;
  284. X    move(my+y, mx+x);
  285. X    return inch() == SCRAP;
  286. X}
  287. END_OF_good.c
  288. if test 2907 -ne `wc -c <good.c`; then
  289.     echo shar: \"good.c\" unpacked with wrong size!
  290. fi
  291. # end of overwriting check
  292. fi
  293. if test -f main.c -a "${1}" != "-c" ; then 
  294.   echo shar: Will not over-write existing file \"main.c\"
  295. else
  296. echo shar: Extracting \"main.c\" \(6448 characters\)
  297. sed "s/^X//" >main.c <<'END_OF_main.c'
  298. X/*
  299. X *    R O B O T S
  300. X *
  301. X *    The game of robots.
  302. X *    History:
  303. X *    Original Implementation
  304. X *        Allan Black <allan@cs.strath.ac.uk>
  305. X *    Updated for fast robots, moveable heaps, ROBOTOPTS
  306. X *    antimatter, v7/sys5/4.2 portability etc.
  307. X *        Graeme Lunt <gal@cs.nott.ac.uk>
  308. X *        Julian Onions <jpo@cs.nott.ac.uk>
  309. X *
  310. X * Provided free as long as you don't make money from it!
  311. X */
  312. X
  313. X# include "robots.h"
  314. X
  315. Xchar    whoami[MAXSTR];
  316. Xchar    my_user_name[MAXSTR];
  317. Xchar    cmd_ch;
  318. X
  319. Xbool    dead = FALSE;
  320. Xbool    last_stand;
  321. Xbool    show_highscore = TRUE;
  322. Xbool    running, adjacent, first_move, bad_move, waiting;
  323. Xbool    moveable_heaps = TRUE;
  324. X
  325. Xint    my_x, my_y;
  326. Xint    new_x, new_y;
  327. Xint    level = 0;
  328. Xint    free_teleports = 0;
  329. Xint    old_free;
  330. Xint    free_per_level = 1;
  331. Xint    count;
  332. Xint    dots = 0;
  333. Xint    robot_value = MIN_VALUE;
  334. Xint    max_robots = MIN_ROBOTS;
  335. Xint    nrobots_alive;
  336. Xint    scrap_heaps = 1;  /* to allow for first level */
  337. X
  338. Xlong    score = 0;
  339. Xlong    seed;
  340. X
  341. X# ifdef TIOCSLTC
  342. Xstruct ltchars    ltc;
  343. Xchar    dsusp;
  344. X# endif TIOCSLTC
  345. X
  346. Xint    interrupt();
  347. X
  348. X# define    TERM_UNINIT    00
  349. X# define    TERM_CURSES    01
  350. X# define    TERM_LTC    02
  351. X
  352. Xint    term_state = TERM_UNINIT;    /* cuts out some race conditions */
  353. Xchar    *getenv();
  354. Xstruct    passwd    *getpwuid();
  355. Xchar    _obuf[BUFSIZ];
  356. X
  357. Xmain(argc,argv)
  358. X    int argc;
  359. X    char *argv[];
  360. X{
  361. X    register struct passwd *pass;
  362. X    register char *x;
  363. X    int i;
  364. X
  365. X    setbuf(stdout, _obuf);
  366. X    if(argc > 1) {
  367. X        if(argv[1][0] == '-') {
  368. X            switch(argv[1][1]) {
  369. X            case 's':
  370. X                show_highscore = TRUE;
  371. X                scoring(FALSE);
  372. X                exit(0);
  373. X            }
  374. X        }
  375. X    }
  376. X    if((pass = getpwuid(getuid())) == 0) {
  377. X        x = "ANON";
  378. X    } else {
  379. X        x = pass->pw_name;
  380. X    }
  381. X    (void) strcpy(my_user_name, x);
  382. X    (void) strcpy(whoami,x);
  383. X    if((x = getenv(ROBOTOPTS)) != NULL && *x != '\0')
  384. X        get_robot_opts(x);
  385. X    seed = time((time_t *)0)+getuid();
  386. X    (void) signal(SIGQUIT,interrupt);
  387. X    (void) signal(SIGINT,interrupt);
  388. X    if( initscr() == ERR) {
  389. X        fprintf(stderr, "Curses won't initialise - seek a guru\n");
  390. X        quit(FALSE);
  391. X    }
  392. X    term_state |= TERM_CURSES;
  393. X    crmode();
  394. X    noecho();
  395. X# ifdef TIOCSLTC
  396. X    (void) ioctl(1,TIOCGLTC,<c);
  397. X    dsusp = ltc.t_dsuspc;
  398. X    ltc.t_dsuspc = ltc.t_suspc;
  399. X    (void) ioctl(1,TIOCSLTC,<c);
  400. X    term_state |= TERM_LTC;
  401. X# endif TIOCSLTC
  402. X    for(;;) {
  403. X        count = 0;
  404. X        running = FALSE;
  405. X        adjacent = FALSE;
  406. X        waiting = FALSE;
  407. X        last_stand = FALSE;
  408. X        old_free = -1;
  409. X        if(rnd(free_per_level) < free_teleports) {
  410. X            free_per_level++;
  411. X            if(free_per_level > MAX_FREE) free_per_level = MAX_FREE;
  412. X        }
  413. X        free_teleports += free_per_level;
  414. X        leaveok(stdscr,FALSE);
  415. X        draw_screen();
  416. X        put_robots();
  417. X        do {
  418. X            my_x = rndx();
  419. X            my_y = rndy();
  420. X            move(my_y,my_x);
  421. X        } while(inch() != ' ');
  422. X        addch(ME);
  423. X        for(;;) {
  424. X            scorer();
  425. X            if(nrobots_alive == 0) break;
  426. X            command();
  427. X            for(i=1;i<=FASTEST;i++)
  428. X                robots(i);
  429. X            if(dead) munch();
  430. X        }
  431. X        msg("%d robots are now %d scrap heaps",max_robots, scrap_heaps);
  432. X        leaveok(stdscr,FALSE);
  433. X        move(my_y,my_x);
  434. X        refresh();
  435. X        (void) readchar();
  436. X        level++;
  437. X    }
  438. X}
  439. X
  440. Xdraw_screen()
  441. X{
  442. X    register int x, y;
  443. X    clear();
  444. X    for(y = 1; y < LINES-2; y++) {
  445. X        mvaddch(y,0,VERT);
  446. X        mvaddch(y,COLS-1,VERT);
  447. X    }
  448. X    for(x = 0; x < COLS; x++) {
  449. X        mvaddch(0,x,HORIZ);
  450. X        mvaddch(LINES-2,x,HORIZ);
  451. X    }
  452. X}
  453. X
  454. Xreadchar()
  455. X{
  456. X    static char buf[1];
  457. X    extern int errno;
  458. X
  459. X    while(read(0,buf,1) != 1)
  460. X        if( errno != EINTR)
  461. X            quit(TRUE);
  462. X    return(buf[0]);
  463. X}
  464. X
  465. Xput_dots()
  466. X{
  467. X    register int x, y;
  468. X    for(x = my_x-dots; x <= my_x+dots; x++) {
  469. X        for(y = my_y-dots; y <= my_y+dots; y++) {
  470. X            move(y,x);
  471. X            if(inch() == ' ') addch(DOT);
  472. X        }
  473. X    }
  474. X}
  475. X
  476. Xerase_dots()
  477. X{
  478. X    register int x, y;
  479. X    for(x = my_x-dots; x <= my_x+dots; x++) {
  480. X        for(y = my_y-dots; y <= my_y+dots; y++) {
  481. X            move(y,x);
  482. X            if(inch() == DOT) addch(' ');
  483. X        }
  484. X    }
  485. X}
  486. X
  487. Xxinc(dir)
  488. X    char dir;
  489. X{
  490. X    switch(dir) {
  491. X    case 'h':
  492. X    case 'y':
  493. X    case 'b':
  494. X        return(-1);
  495. X    case 'l':
  496. X    case 'u':
  497. X    case 'n':
  498. X        return(1);
  499. X    case 'j':
  500. X    case 'k':
  501. X    default:
  502. X        return(0);
  503. X    }
  504. X}
  505. X
  506. Xyinc(dir)
  507. X    char dir;
  508. X{
  509. X    switch(dir) {
  510. X    case 'k':
  511. X    case 'y':
  512. X    case 'u':
  513. X        return(-1);
  514. X    case 'j':
  515. X    case 'b':
  516. X    case 'n':
  517. X        return(1);
  518. X    case 'h':
  519. X    case 'l':
  520. X    default:
  521. X        return(0);
  522. X    }
  523. X}
  524. X
  525. Xmunch()
  526. X{
  527. X    scorer();
  528. X    msg("MUNCH! You're robot food");
  529. X    leaveok(stdscr,FALSE);
  530. X    mvaddch(my_y,my_x,MUNCH);
  531. X    move(my_y,my_x);
  532. X    refresh();
  533. X    (void) readchar();
  534. X    quit(TRUE);
  535. X}
  536. X
  537. Xquit(eaten)
  538. X    bool eaten;
  539. X{
  540. X    if( term_state & TERM_CURSES ) {
  541. X        move(LINES-1,0);
  542. X        refresh();
  543. X        endwin();
  544. X        term_state &= ~ TERM_CURSES;
  545. X    }
  546. X    putchar('\n');
  547. X# ifdef TIOCSLTC
  548. X    if( term_state & TERM_LTC ) {
  549. X        ltc.t_dsuspc = dsusp;
  550. X        (void) ioctl(1,TIOCSLTC,<c);
  551. X        term_state &= ~ TERM_LTC;
  552. X    }
  553. X# endif TIOCSLTC
  554. X    (void) signal(SIGINT, SIG_DFL);
  555. X    scoring(eaten);
  556. X    exit(0);
  557. X}
  558. X
  559. Xrndx()
  560. X{
  561. X    return(rnd(COLS-2)+1);
  562. X}
  563. X
  564. Xrndy()
  565. X{
  566. X    return(rnd(LINES-3)+1);
  567. X}
  568. X
  569. Xrnd(mod)
  570. X    int mod;
  571. X{
  572. X    if(mod <= 0) return(0);
  573. X    return((((seed = seed*11109L+13849L) >> 16) & 0xffffL) % mod);
  574. X}
  575. X
  576. X/* VARARGS 1 */
  577. Xmsg(message,a1, a2, a3, a4, a5, a6, a7)
  578. X    char *message;
  579. X    unsigned int a1, a2, a3, a4, a5, a6, a7;
  580. X{
  581. X    static char msgbuf[1000];
  582. X
  583. X    (void) sprintf(msgbuf, message, a1, a2, a3, a4, a5, a6, a7);
  584. X    mvaddstr(LINES-1,MSGPOS,msgbuf);
  585. X    clrtoeol();
  586. X    refresh();
  587. X}
  588. X
  589. Xinterrupt()
  590. X{
  591. X    quit(FALSE);
  592. X}
  593. X
  594. X/*
  595. X * file locking routines - much nicer under BSD ...
  596. X */
  597. X
  598. X# ifdef BSD42
  599. Xlk_open(file, mode)    /* lock a file using the flock sys call */
  600. Xchar    *file;
  601. Xint    mode;
  602. X{
  603. X    int    fd;
  604. X
  605. X    if( (fd = open(file, mode)) < 0)
  606. X        return -1;
  607. X    if( flock(fd, LOCK_EX) < 0)
  608. X    {
  609. X        (void) close(fd);
  610. X        return -1;
  611. X    }
  612. X    return fd;
  613. X}
  614. X
  615. Xlk_close( fd, file)
  616. Xint    fd;
  617. Xchar    *file;
  618. X{
  619. X# ifdef lint
  620. X    file = file;    /* now will you shut up lint???? */
  621. X# endif
  622. X    return close(fd);
  623. X}
  624. X# else
  625. X
  626. X# define    LOCKTIME    (60)    /* 1 minute */
  627. X# include    <sys/stat.h>
  628. X
  629. Xlk_open(file, mode)    /* lock a file by crude means */
  630. Xchar    *file;
  631. Xint    mode;
  632. X{
  633. X    char    tfile[128], lfile[128];
  634. X    struct    stat stbuf;
  635. X    time_t    now;
  636. X    int    fd;
  637. X
  638. X    (void) sprintf(tfile, "%s.t", file);    /* temp file */
  639. X    (void) sprintf(lfile, "%s.l", file);    /* lock file */
  640. X
  641. X    if( close(creat(tfile, 0)) < 0)    /* make temp file */
  642. X        return -1;
  643. X    while( link(tfile, lfile) == -1) /* now attempt the lock file */
  644. X    {
  645. X        if( stat(lfile, &stbuf) < 0)
  646. X            continue;    /* uhh? -- try again */
  647. X        time(&now);
  648. X        /* OK - is this file old? */
  649. X        if( stbuf.st_mtime + LOCKTIME < now)
  650. X            unlink(lfile);    /* ok its old enough - junk it */
  651. X        else    sleep(1);    /* snooze ... */
  652. X    }
  653. X    unlink(tfile);    /* tmp files done its job */
  654. X    if((fd = open(file, mode)) < 0) {
  655. X        unlink(lfile);
  656. X        return -1;
  657. X    }
  658. X    return fd;
  659. X}
  660. X
  661. Xlk_close(fd, fname)
  662. Xint    fd;
  663. Xchar    *fname;
  664. X{
  665. X    char    lfile[128];
  666. X
  667. X    (void) sprintf(lfile, "%s.l", fname);    /* recreate the lock file name */
  668. X    if( unlink(lfile) == -1)    /* blow it away */
  669. X        perror(lfile);
  670. X    return close(fd);
  671. X}
  672. X# endif
  673. X
  674. END_OF_main.c
  675. if test 6448 -ne `wc -c <main.c`; then
  676.     echo shar: \"main.c\" unpacked with wrong size!
  677. fi
  678. # end of overwriting check
  679. fi
  680. if test -f makelog -a "${1}" != "-c" ; then 
  681.   echo shar: Will not over-write existing file \"makelog\"
  682. else
  683. echo shar: Extracting \"makelog\" \(217 characters\)
  684. sed "s/^X//" >makelog <<'END_OF_makelog'
  685. Xcc -DBSD42  -c good.c
  686. Xcc -DBSD42  -c main.c
  687. Xcc -DBSD42  -c opt.c
  688. Xcc -DBSD42  -c robot.c
  689. Xcc -DBSD42  -c score.c
  690. Xcc -DBSD42  -c user.c
  691. Xcc -DBSD42  -o robots good.o main.o opt.o robot.o score.o user.o -lcurses -ltermcap
  692. END_OF_makelog
  693. if test 217 -ne `wc -c <makelog`; then
  694.     echo shar: \"makelog\" unpacked with wrong size!
  695. fi
  696. # end of overwriting check
  697. fi
  698. if test -f opt.c -a "${1}" != "-c" ; then 
  699.   echo shar: Will not over-write existing file \"opt.c\"
  700. else
  701. echo shar: Extracting \"opt.c\" \(1651 characters\)
  702. sed "s/^X//" >opt.c <<'END_OF_opt.c'
  703. X# include "robots.h"
  704. X
  705. X/*
  706. X * opt.c: do a psuedo ROGUEOPTS sort of thing
  707. X */
  708. X
  709. Xtypedef struct Opt {
  710. X    char    *name;
  711. X    int    type;
  712. X} Opt;
  713. X
  714. XOpt    options[] = {
  715. X# define    OPT_NAME    1
  716. X    "name",        OPT_NAME,    /* Who am i boss ? */
  717. X# define    OPT_MOVE_HEAP    2
  718. X    "moveheaps",    OPT_MOVE_HEAP,    /* can push heaps */
  719. X# define    OPT_NOMOVE_HEAP    3
  720. X    "nomoveheaps",    OPT_NOMOVE_HEAP,/* can't push heaps */
  721. X# define    OPT_SHOW_HSCORE 4
  722. X    "showhscore",    OPT_SHOW_HSCORE,
  723. X# define    OPT_NOSHOW_HSCORE 5
  724. X    "noshowhscore", OPT_NOSHOW_HSCORE,
  725. X    0,        0
  726. X};
  727. X
  728. X/* get_robot_opts: Personalise robots to the users tastes. Model after
  729. X * the rogue/urogue type environment stuff. 
  730. X */
  731. X
  732. Xget_robot_opts(str)
  733. Xchar    *str;
  734. X{
  735. X    register char     *p;
  736. X    Opt    *op;
  737. X    int    len, len2;
  738. X
  739. X    p = str;
  740. X    while(*p)
  741. X    {
  742. X        while(*p && !isalpha(*p)) p++;  /* skip non-alphas */
  743. X        str = p;
  744. X
  745. X        while(isalpha(*p)) p ++;    /* match longest word */
  746. X        len = p - str;
  747. X
  748. X        for(op = options; op->name; op ++)    /* see if defined */
  749. X            if( strncmp(str, op->name, len) == 0)
  750. X                break;
  751. X        if( op->name == NULL)
  752. X            continue;
  753. X        switch(op->type)    /* OK, now do something */
  754. X        {
  755. X            case OPT_NAME:
  756. X                while(*p == '=') p++;    /* skip ='s */
  757. X                str = p;    /* OK, now look for name */
  758. X                while(*p && *p != ',') p++;
  759. X                len2 = (MAXSTR - 1) -
  760. X                    (strlen(my_user_name) + 4);
  761. X                len = p - str;
  762. X                len = len < len2 ? len : len2;
  763. X                (void) sprintf(whoami, "%.*s (%s)", len,
  764. X                    str, my_user_name);
  765. X                break;
  766. X            case    OPT_MOVE_HEAP:
  767. X                moveable_heaps = TRUE;
  768. X                break;
  769. X            case    OPT_NOMOVE_HEAP:
  770. X                moveable_heaps = FALSE;
  771. X                break;
  772. X            case    OPT_SHOW_HSCORE:
  773. X                show_highscore = TRUE;
  774. X                break;
  775. X            case    OPT_NOSHOW_HSCORE:
  776. X                show_highscore = FALSE;
  777. X                break;
  778. X        }
  779. X    }
  780. X}
  781. X
  782. END_OF_opt.c
  783. if test 1651 -ne `wc -c <opt.c`; then
  784.     echo shar: \"opt.c\" unpacked with wrong size!
  785. fi
  786. # end of overwriting check
  787. fi
  788. if test -f robot.c -a "${1}" != "-c" ; then 
  789.   echo shar: Will not over-write existing file \"robot.c\"
  790. else
  791. echo shar: Extracting \"robot.c\" \(3145 characters\)
  792. sed "s/^X//" >robot.c <<'END_OF_robot.c'
  793. X# include "robots.h"
  794. X
  795. X/*
  796. X * robot.c: most of the robot oriented stuff
  797. X */
  798. X
  799. Xstruct robot {
  800. X    bool    alive;    /* is this suker still kicking */
  801. X    int    x;
  802. X    int    y;
  803. X    int    speed;  /* speed of robot 1 or 2 for now */
  804. X} rbt_list[MAX_ROBOTS+1];
  805. X
  806. Xput_robots()    /* place some robots */
  807. X{
  808. X    register struct robot *r, *end;
  809. X    register int x, y;
  810. X
  811. X    robot_value += level*(5 + (max_robots/scrap_heaps));
  812. X    max_robots += level*3+rnd(level*5);
  813. X    if(max_robots > MAX_ROBOTS) max_robots = MAX_ROBOTS;
  814. X    nrobots_alive = max_robots;
  815. X    scrap_heaps = 0;    /* number of scrap heaps created */
  816. X    end = &rbt_list[max_robots];
  817. X    for(r = rbt_list; r < end; r++) {
  818. X        for(;;) {
  819. X            x = rndx();
  820. X            y = rndy();
  821. X            move(y,x);
  822. X            if(inch() == ' ') break;
  823. X        }
  824. X        r->x = x;
  825. X        r->y = y;
  826. X        r->alive = TRUE;
  827. X        r->speed = 1 +  ( rnd(10) > 6 );
  828. X        if (r->speed == 2) addch(FROBOT);
  829. X        else    addch(ROBOT);
  830. X    }
  831. X}
  832. X
  833. Xrobots(speed)    /* Troops, Troooooops advance! */
  834. Xint speed;
  835. X{
  836. X    register struct robot *r, *end;
  837. X
  838. X    end = &rbt_list[max_robots];
  839. X    for(r = rbt_list; r < end; r++) {
  840. X        if(r->alive && r->speed >= speed ) {
  841. X            mvaddch(r->y,r->x,' ');
  842. X        }
  843. X    }
  844. X    for(r = rbt_list; r < end; r++) {
  845. X        if (r->alive && r->speed >= speed )
  846. X        {
  847. X            r->x += sign(my_x-r->x);
  848. X            r->y += sign(my_y-r->y);
  849. X            move(r->y,r->x);
  850. X            switch(inch()) {
  851. X            case ME:    /* robot eat me */
  852. X                addch(MUNCH);
  853. X                dead = TRUE;
  854. X                break;
  855. X            case SCRAP:    /* robot walks into scrap heap */
  856. X                r->alive = FALSE;
  857. X                if(r->speed == 2)
  858. X                    score += (MULT * robot_value);
  859. X                else    score += robot_value;
  860. X                nrobots_alive--;
  861. X                break;
  862. X            case FROBOT:
  863. X            case ROBOT:    /* two robots form scrap heap */
  864. X                collision(r, end);
  865. X                r->alive = FALSE;
  866. X                addch(SCRAP);
  867. X                if (r->speed == 2)
  868. X                    score += (MULT * robot_value);
  869. X                else    score += robot_value;
  870. X                nrobots_alive -= 2;
  871. X                scrap_heaps++ ;
  872. X                break;
  873. X            case MUNCH:
  874. X                break;
  875. X            default:
  876. X                if(r->speed == 2) addch(FROBOT);
  877. X                else    addch(ROBOT);
  878. X            }
  879. X        }
  880. X    }
  881. X}
  882. X
  883. X/* do two robots collide - if so - which two */
  884. Xcollision(r, end)
  885. Xregister struct robot *r, *end;
  886. X{
  887. X    register struct robot *find;
  888. X
  889. X    for(find = rbt_list; find < end; find++) {
  890. X        if(find->alive && r != find) {
  891. X            if(r->x == find->x && r->y == find->y) {
  892. X                find->alive = FALSE;
  893. X                if (find->speed == 2)
  894. X                    score += (MULT * robot_value);
  895. X                else score += robot_value;
  896. X                return;
  897. X            } /* end of if */
  898. X        }
  899. X    }
  900. X}
  901. X
  902. Xscrewdriver()    /* dismantle those robots ... */
  903. X{
  904. X    register int test_x, test_y;
  905. X    register char *m ;
  906. X    register struct robot *end,*find;
  907. X    static char moves[] = "hjklyubn.";
  908. X
  909. X    end = &rbt_list[max_robots];
  910. X
  911. X    for(m = moves; *m; m++) {    /* let's see if there is a robot */
  912. X        test_x = my_x+xinc(*m);
  913. X        test_y = my_y+yinc(*m);
  914. X        move(test_y,test_x);
  915. X        switch(inch()) {
  916. X        case FROBOT:
  917. X        case ROBOT:    /* OK so there is now let's look for it */
  918. X            for(find = rbt_list; find < end; find++) {
  919. X                if(find->alive) {
  920. X                    if(test_x == find->x && test_y  == find->y) {
  921. X                        find->alive = FALSE;
  922. X                        if (find->speed == 2) score += (MULT * robot_value);
  923. X                        else score += robot_value;
  924. X                        } /* end of if */
  925. X                } /* end of if */
  926. X            } /* end of for */
  927. X            addch(' ');
  928. X            nrobots_alive--;
  929. X            break;
  930. X        } /* end of case */
  931. X    }
  932. X    free_teleports--;
  933. X}
  934. END_OF_robot.c
  935. if test 3145 -ne `wc -c <robot.c`; then
  936.     echo shar: \"robot.c\" unpacked with wrong size!
  937. fi
  938. # end of overwriting check
  939. fi
  940. if test -f robots.6 -a "${1}" != "-c" ; then 
  941.   echo shar: Will not over-write existing file \"robots.6\"
  942. else
  943. echo shar: Extracting \"robots.6\" \(3170 characters\)
  944. sed "s/^X//" >robots.6 <<'END_OF_robots.6'
  945. X.TH ROBOTS 6 "2 November 1984"
  946. X.SH NAME
  947. Xrobots \- a game of logic
  948. X.SH SYNOPSIS
  949. X.B /usr/games/robots [-s]
  950. X.SH DESCRIPTION
  951. X.B Robots
  952. Xis a display-based game which must be played on a CRT terminal
  953. Xfrom among those supported by vi(1).
  954. XThe object of the game is to avoid the robots:
  955. Xcausing them to collide with each other, creating a
  956. X.IR scrap\ heap .
  957. XRobots are also destroyed if they run into a
  958. X.IR scrap\ heap .
  959. X.PP
  960. XYou are represented on the screen by an I.
  961. XThe robots are represented by `=' and `#' signs.
  962. XScrap heaps are represented by `@' signs.
  963. XAfter each move, all robots move one step towards you except for
  964. Xthe `#' robots which move at twice the speed of normal robots.
  965. X.PP
  966. XThe following commands are recognized:
  967. X.IP h
  968. XMove left.
  969. X.IP l
  970. XMove right.
  971. X.IP j
  972. XMove down.
  973. X.IP k
  974. XMove up.
  975. X.IP y
  976. XMove up and left.
  977. X.IP u
  978. XMove up and right.
  979. X.IP b
  980. XMove down and left.
  981. X.IP n
  982. XMove down and right.
  983. X.IP .
  984. X.IP w
  985. XDo nothing.
  986. X.IP t
  987. XTeleport to a safe square if possible (limited in number).
  988. X.IP r
  989. XTeleport to a random position.
  990. X.IP a
  991. XAntimatter, all robots one square away from you vanish.
  992. X.IP s
  993. X.IP W
  994. XSit and watch.
  995. X.IP d
  996. XDisplay dots.
  997. X.IP m
  998. XPrint list of `safe' moves.
  999. X.IP ?
  1000. XSame as above.
  1001. X.IP q
  1002. XQuit game.
  1003. X.IP ^R
  1004. XRedraw the screen.
  1005. X.IP ^Z
  1006. XSuspend the game, on systems which support it.
  1007. X.IP ^W
  1008. XSit and watch while safe to do so.
  1009. X.PP
  1010. XA command may be preceeded by a count where relevant.
  1011. X.I Shift\-direction
  1012. Xmoves as far as possible in that direction.
  1013. X.I Control\-direction
  1014. Xmoves until adjacent to something.
  1015. X.PP
  1016. XA record is kept of the personal best scores of each player:
  1017. Xone for the current week and one for all time.
  1018. XScores are printed out at the end of the game.
  1019. XThe scores can be printed out with the '\-s' parameter.
  1020. X.SH CONFIGURATION
  1021. XIf you have in your environment the variable
  1022. X.I ROBOTOPTS
  1023. Xthen this is examined for a comma separated list of options to be set.
  1024. XBinary options can be preceeded with `no' to reverse the meaning.
  1025. XValid options are
  1026. X.IP name=string
  1027. XSet your name to the given string, this is recorded in the high
  1028. Xscore table. Your name appears as `string (username)'.
  1029. X.IP moveheaps
  1030. XMake the scrap heaps movable. If heaps are movable, then
  1031. Xa scrap heap can be pushed in any direction provided there is nothing
  1032. Xbehind the heap. This may or may not be the default.
  1033. X.IP showhscores
  1034. XThis option shows the highscore table after every game. The
  1035. Xnegated version will only print out the highscore only if you appear
  1036. Xon it.
  1037. X.SH NOTE
  1038. XThe first time the `t' command is used on a new level
  1039. Xyou are guaranteed not to land next to a robot, a count of
  1040. Xthese `safe' teleports is maintained in the bottom left hand corner.
  1041. X`Antimatters' use up a `safe' teleport.
  1042. X.br
  1043. XAn asterisk in the first set of scores indicates an out-of-date entry.
  1044. X.br
  1045. XThe value of each robot in the next round is inversely
  1046. Xproportional to the
  1047. Xnumber of scrap heaps created in the previous round.
  1048. XDouble speed robots count 1.5 times the value of normal robots.
  1049. X.SH FILES
  1050. X.nf
  1051. X/usr/games/lib/robots_tmp    this week's best scores
  1052. X/usr/games/lib/robots_hof    all time best scores
  1053. X.fi
  1054. X.SH AUTHOR
  1055. XAllan Black, Strathclyde University, Glasgow.
  1056. X.br
  1057. XEnhancements by Graeme Lunt & Julian Onions, Nottingham University.
  1058. END_OF_robots.6
  1059. if test 3170 -ne `wc -c <robots.6`; then
  1060.     echo shar: \"robots.6\" unpacked with wrong size!
  1061. fi
  1062. # end of overwriting check
  1063. fi
  1064. if test -f robots.h -a "${1}" != "-c" ; then 
  1065.   echo shar: Will not over-write existing file \"robots.h\"
  1066. else
  1067. echo shar: Extracting \"robots.h\" \(2357 characters\)
  1068. sed "s/^X//" >robots.h <<'END_OF_robots.h'
  1069. X/*
  1070. X * robots.h: include file for the robots game
  1071. X */
  1072. X
  1073. X# include <curses.h>
  1074. X# include <signal.h>
  1075. X# include <pwd.h>
  1076. X# include <ctype.h>
  1077. X# include <sys/types.h>
  1078. X# include <errno.h>
  1079. X# ifdef    BSD42
  1080. X# include <sys/file.h>
  1081. X# endif BSD42
  1082. X
  1083. X# define MIN_ROBOTS    10    /* no. of robots you start with */
  1084. X# define MAX_ROBOTS    500    /* maximum robots on a screen    */
  1085. X# define MIN_VALUE    10    /* what each robot is worth to begin */
  1086. X# define MAX_FREE    3    /* max free teleports per level    */
  1087. X# define FASTEST    2    /* the fastest robot (dont fiddle) */
  1088. X
  1089. X# define VERT    '|'        /* vertical wall    */
  1090. X# define HORIZ    '-'        /* horizontal wall    */
  1091. X# define ROBOT    '='        /* normal robot        */
  1092. X# define FROBOT '#'        /* fast robot        */
  1093. X# define SCRAP  '@'
  1094. X# define ME    'I'
  1095. X# define MUNCH    '*'
  1096. X# define DOT    '.'
  1097. X
  1098. X# define LEVEL        (level+1)
  1099. X
  1100. X# define MSGPOS        35    /* where messages appear on bottom line */
  1101. X# define RVPOS        47
  1102. X
  1103. X/* These you may want to fiddle with. Position of the two high score files */
  1104. X# define HOF_FILE    "/usr/sheriff/jpo/games/lib/robots_hof"
  1105. X# define TMP_FILE    "/usr/sheriff/jpo/games/lib/robots_tmp"
  1106. X
  1107. X# define NUMSCORES    20        /* number of people to record */
  1108. X# define NUMNAME    "Twenty"    /* above spelt out */
  1109. X
  1110. X# define TEMP_DAYS    7        /* how long temp scores last */
  1111. X# define TEMP_NAME    "Week"
  1112. X
  1113. X# define ROBOTOPTS    "ROBOTOPTS"    /* environment tailoring */
  1114. X
  1115. X# define MAXSTR        100
  1116. X
  1117. X# define MULT        1.5        /* multiplier for fast robots */
  1118. X
  1119. X/* if ALLSCORES Undefined - record top n players */
  1120. X# define ALLSCORES            /* record top n scores */
  1121. X
  1122. X# define SECSPERDAY    86400
  1123. X
  1124. X# define ctrl(x)    ((x)&037)
  1125. X# define BEL    ctrl('G')
  1126. X
  1127. X# define    abs(X)  ((X) < 0 ? -(X) : (X))
  1128. X# define    sign(X) ((X) < 0 ? -1 : (X) > 0)
  1129. X
  1130. Xextern    char    whoami[];
  1131. Xextern    char    my_user_name[];
  1132. Xextern    char    cmd_ch;
  1133. X
  1134. Xextern    bool    moveable_heaps;
  1135. Xextern    bool    show_highscore;
  1136. Xextern    bool    last_stand;
  1137. Xextern    bool    bad_move;
  1138. Xextern    bool    running;
  1139. Xextern    bool    waiting;
  1140. Xextern    bool    first_move;
  1141. Xextern    bool    adjacent;
  1142. Xextern    bool    dead;
  1143. X
  1144. Xextern    int    my_x, my_y;
  1145. Xextern    int    new_x, new_y;
  1146. Xextern    int    count;
  1147. Xextern    int    free_teleports;
  1148. Xextern    int     dots;
  1149. Xextern    int    robot_value;
  1150. Xextern    int    level;
  1151. Xextern    int    max_robots;
  1152. Xextern    int    scrap_heaps;
  1153. Xextern    int    nrobots_alive;
  1154. Xextern    int    free_per_level;
  1155. Xextern    int    old_free;
  1156. X
  1157. Xextern    long    score;
  1158. Xextern    long    lseek();
  1159. X
  1160. Xextern    char    *strcpy ();
  1161. Xextern    char    *strncpy ();
  1162. Xextern    char    *malloc ();
  1163. Xextern    char    *gets ();
  1164. Xextern    char    *sprintf ();
  1165. END_OF_robots.h
  1166. if test 2357 -ne `wc -c <robots.h`; then
  1167.     echo shar: \"robots.h\" unpacked with wrong size!
  1168. fi
  1169. # end of overwriting check
  1170. fi
  1171. if test -f score.c -a "${1}" != "-c" ; then 
  1172.   echo shar: Will not over-write existing file \"score.c\"
  1173. else
  1174. echo shar: Extracting \"score.c\" \(4532 characters\)
  1175. sed "s/^X//" >score.c <<'END_OF_score.c'
  1176. X# include "robots.h"
  1177. X
  1178. X/*
  1179. X * score.c: All the scoring code is in here.
  1180. X */
  1181. X
  1182. Xstruct scorefile {
  1183. X    int    s_uid;
  1184. X    long    s_score;
  1185. X    char    s_name[MAXSTR];
  1186. X    bool    s_eaten;
  1187. X    int    s_level;
  1188. X    int    s_days;
  1189. X};
  1190. X
  1191. X# define FILE_SIZE    (NUMSCORES*sizeof(struct scorefile))
  1192. X
  1193. Xscoring(eaten)
  1194. X    bool eaten;
  1195. X{
  1196. X    static char buf[MAXSTR];
  1197. X
  1198. X    (void) sprintf(buf,"for this %s",TEMP_NAME);
  1199. X    if( record_score(eaten,TMP_FILE,TEMP_DAYS,buf) || show_highscore) {
  1200. X        printf("[Press return to continue]");
  1201. X        fflush(stdout);
  1202. X        gets(buf);
  1203. X    }
  1204. X    record_score(eaten,HOF_FILE,0,"of All Time");
  1205. X}
  1206. X
  1207. X# define    sigbit(x)    (1 << ((x) - 1))
  1208. X
  1209. Xrecord_score(eaten,fname,max_days,type_str)
  1210. X    bool eaten;
  1211. X    char *fname;
  1212. X    int max_days;
  1213. X    char *type_str;
  1214. X{
  1215. X    int value;
  1216. X    int fd;
  1217. X    int omask;
  1218. X
  1219. X    /* block signals while recording the score 
  1220. X     * hope this routine doesn't get stuck! 
  1221. X     */
  1222. X# ifndef BSD42
  1223. X    int    (*oint)(), (*oterm)(), (*ohup)();
  1224. X
  1225. X    oint = signal(SIGINT, SIG_IGN);
  1226. X    oterm = signal(SIGTERM, SIG_IGN);
  1227. X    ohup = signal(SIGHUP, SIG_IGN);
  1228. X# else
  1229. X    omask = sigblock( sigbit(SIGINT) | sigbit(SIGTERM) | sigbit(SIGHUP) 
  1230. X            | sigbit(SIGTSTP));
  1231. X# endif
  1232. X    
  1233. X    if((fd = lk_open(fname,2)) < 0) {
  1234. X        perror(fname);
  1235. X    } else {
  1236. X        value = do_score(eaten,fd,max_days,type_str);
  1237. X        lk_close(fd, fname);
  1238. X    }
  1239. X# ifdef BSD42
  1240. X    (void) sigsetmask(omask);
  1241. X# else
  1242. X    (void) signal(SIGINT, oint);
  1243. X    (void) signal(SIGTERM, oterm);
  1244. X    (void) signal(SIGHUP, ohup);
  1245. X# endif
  1246. X    return value;
  1247. X}
  1248. X
  1249. Xdo_score(eaten,fd,max_days,type_str)
  1250. X    bool eaten;
  1251. X    int fd, max_days;
  1252. X    char *type_str;
  1253. X{
  1254. X    register struct scorefile *position;
  1255. X    register int x;
  1256. X    register struct scorefile *remove, *sfile, *eof;
  1257. X    struct scorefile *oldest, *this;
  1258. X    int uid, this_day, limit;
  1259. X
  1260. X    this_day = max_days ? time((time_t *)0)/SECSPERDAY : 0;
  1261. X    limit = this_day-max_days;
  1262. X    sfile = (struct scorefile *)(malloc(FILE_SIZE));
  1263. X    if( sfile == NULL)
  1264. X    {
  1265. X        fprintf( stderr, "Out of memmory so no scoring");
  1266. X        return FALSE;
  1267. X    }
  1268. X    eof = &sfile[NUMSCORES];
  1269. X    this = 0;
  1270. X    for(position = sfile; position < eof; position++) {
  1271. X        position->s_score = 0;
  1272. X        position->s_days = 0;
  1273. X    }
  1274. X    read(fd, (char *)sfile,FILE_SIZE);
  1275. X    remove = 0;
  1276. X    if(score > 0) {
  1277. X        uid = getuid();
  1278. X        oldest = 0;
  1279. X        x = limit;
  1280. X        for(position = eof-1; position >= sfile; position--) {
  1281. X            if(position->s_days < x) {
  1282. X                x = position->s_days;
  1283. X                oldest = position;
  1284. X            }
  1285. X        }
  1286. X        position = 0;
  1287. X        for(remove = sfile; remove < eof; remove++) {
  1288. X            if(position == 0 && score > remove->s_score) position = remove;
  1289. X# ifndef ALLSCORES
  1290. X            if(remove->s_uid == uid) break;
  1291. X# endif ALLSCORES
  1292. X        }
  1293. X        if(remove < eof) {
  1294. X            if(position == 0 && remove->s_days < limit) position = remove;
  1295. X        } else if(oldest) {
  1296. X            remove = oldest;
  1297. X            if(position == 0) {
  1298. X                position = eof-1;
  1299. X            } else if(remove < position) {
  1300. X                position--;
  1301. X            }
  1302. X        } else if(position) {
  1303. X            remove = eof-1;
  1304. X        }
  1305. X        if(position) {
  1306. X            if(remove < position) {
  1307. X                while(remove < position) {
  1308. X                    *remove = *(remove+1);
  1309. X                    remove++;
  1310. X                }
  1311. X            } else {
  1312. X                while(remove > position) {
  1313. X                    *remove = *(remove-1);
  1314. X                    remove--;
  1315. X                }
  1316. X            }
  1317. X            position->s_score = score;
  1318. X            (void) strncpy(position->s_name,whoami,MAXSTR);
  1319. X            position->s_eaten = eaten;
  1320. X            position->s_level = LEVEL;
  1321. X            position->s_uid = uid;
  1322. X            position->s_days = this_day;
  1323. X            this = position;
  1324. X
  1325. X            if(lseek(fd,0L,0) == -1L ||
  1326. X                write(fd,(char *)sfile,FILE_SIZE) != FILE_SIZE)
  1327. X                perror("scorefile");
  1328. X            close(fd);
  1329. X        }
  1330. X    }
  1331. X    if( show_highscore || this )
  1332. X    {
  1333. X        printf(
  1334. X# ifdef ALLSCORES
  1335. X            "\nTop %s Scores %s:\n",
  1336. X# else ALLSCORES
  1337. X            "\nTop %s Robotists %s:\n",
  1338. X# endif ALLSCORES
  1339. X            NUMNAME,
  1340. X            type_str
  1341. X        );
  1342. X        printf("Rank   Score    Name\n");
  1343. X        for(position = sfile; position < eof; position++) {
  1344. X            if(position->s_score == 0) break;
  1345. X            if(position == this)
  1346. X                putchar('>');
  1347. X            else  putchar(' ');
  1348. X            printf(
  1349. X                "%c%2d %9ld   %s: %s on level %d.",
  1350. X                position->s_days < limit ? '*' : ' ',
  1351. X                position-sfile+1,
  1352. X                position->s_score,
  1353. X                position->s_name,
  1354. X                position->s_eaten ? "eaten" : "chickened out",
  1355. X                position->s_level
  1356. X            );
  1357. X            if(position == this)
  1358. X                putchar('<');
  1359. X            putchar('\n');
  1360. X        }
  1361. X    }
  1362. X    return (this != 0);
  1363. X}
  1364. X
  1365. Xscorer()
  1366. X{
  1367. X    static char tels[6];
  1368. X    if(free_teleports != old_free) {
  1369. X        if(free_teleports > free_per_level) {
  1370. X            (void) sprintf(tels,"%d+%d",
  1371. X                       free_per_level,
  1372. X                       free_teleports-free_per_level);
  1373. X        } else {
  1374. X            (void) sprintf(tels,"%d",free_teleports);
  1375. X        }
  1376. X        old_free = free_teleports;
  1377. X    }
  1378. X    move(LINES-1,0);
  1379. X    clrtoeol();
  1380. X    printw("<%s> level: %d    score: %ld",tels,LEVEL,score);
  1381. X        mvprintw(LINES-1,RVPOS,"heaps:%3d robots:%3d value: %d",
  1382. X                    scrap_heaps,nrobots_alive,robot_value);
  1383. X    clrtoeol();
  1384. X}
  1385. X
  1386. END_OF_score.c
  1387. if test 4532 -ne `wc -c <score.c`; then
  1388.     echo shar: \"score.c\" unpacked with wrong size!
  1389. fi
  1390. # end of overwriting check
  1391. fi
  1392. if test -f user.c -a "${1}" != "-c" ; then 
  1393.   echo shar: Will not over-write existing file \"user.c\"
  1394. else
  1395. echo shar: Extracting \"user.c\" \(3604 characters\)
  1396. sed "s/^X//" >user.c <<'END_OF_user.c'
  1397. X# include "robots.h"
  1398. X
  1399. X/*
  1400. X * user.c: user oriented things
  1401. X */
  1402. X
  1403. Xcommand()    /* whats the user trying to tell us */
  1404. X{
  1405. Xretry:
  1406. X    move(my_y,my_x);
  1407. X    refresh();
  1408. X    if(last_stand) return;
  1409. X    bad_move = FALSE;
  1410. X    if(!running) {
  1411. X        cmd_ch = read_com();
  1412. X        switch(cmd_ch) {
  1413. X        case ctrl('W'):
  1414. X            waiting = TRUE;
  1415. X        case ctrl('H'):
  1416. X        case ctrl('J'):
  1417. X        case ctrl('K'):
  1418. X        case ctrl('L'):
  1419. X        case ctrl('Y'):
  1420. X        case ctrl('U'):
  1421. X        case ctrl('B'):
  1422. X        case ctrl('N'):
  1423. X            cmd_ch |= 0100;
  1424. X            adjacent = TRUE;
  1425. X        case 'H':
  1426. X        case 'J':
  1427. X        case 'K':
  1428. X        case 'L':
  1429. X        case 'Y':
  1430. X        case 'U':
  1431. X        case 'B':
  1432. X        case 'N':
  1433. X            cmd_ch |= 040;
  1434. X            running = TRUE;
  1435. X            first_move = TRUE;
  1436. X        case 't':
  1437. X        case 'T':
  1438. X        case 's':
  1439. X        case 'S':
  1440. X        case 'W':
  1441. X        case 'm':
  1442. X        case 'M':
  1443. X        case '?':
  1444. X        case 'd':
  1445. X        case 'D':
  1446. X        case ctrl('R'):
  1447. X            count = 0;
  1448. X        }
  1449. X    }
  1450. X    switch(cmd_ch) {
  1451. X    case '.':
  1452. X    case 'h':
  1453. X    case 'j':
  1454. X    case 'k':
  1455. X    case 'l':
  1456. X    case 'y':
  1457. X    case 'u':
  1458. X    case 'b':
  1459. X    case 'n':
  1460. X    case 'w':
  1461. X        do_move(cmd_ch);
  1462. X        break;
  1463. X    case 't':
  1464. X    case 'r':
  1465. X    case 'T':
  1466. X    case 'R':
  1467. X    teleport:
  1468. X        new_x = rndx();
  1469. X        new_y = rndy();
  1470. X        move(new_y,new_x);
  1471. X        switch(inch()) {
  1472. X        case FROBOT:
  1473. X        case ROBOT:
  1474. X        case SCRAP:
  1475. X        case ME:
  1476. X            goto teleport;
  1477. X        }
  1478. X        if( (free_teleports > 0)
  1479. X           && ((cmd_ch == 't')||(cmd_ch =='T')) ) {
  1480. X            if( !isgood(new_y, new_x))
  1481. X                goto teleport;
  1482. X            free_teleports--;
  1483. X        }
  1484. X        break;
  1485. X    case 's':
  1486. X    case 'S':
  1487. X    case 'W':
  1488. X        last_stand = TRUE;
  1489. X        leaveok(stdscr,TRUE);
  1490. X        return;
  1491. X    case 'm':
  1492. X    case 'M':
  1493. X    case '?':
  1494. X        good_moves();
  1495. X        goto retry;
  1496. X    case 'd':
  1497. X    case 'D':
  1498. X        if(dots < 2) {
  1499. X            dots++;
  1500. X            put_dots();
  1501. X        } else {
  1502. X            erase_dots();
  1503. X            dots = 0;
  1504. X        }
  1505. X        goto retry;
  1506. X    case 'q':
  1507. X    case 'Q':
  1508. X        quit(FALSE);
  1509. X    case 'a':
  1510. X    case 'A':    /* Antimatter - sonic screwdriver */
  1511. X        if (free_teleports) { new_x = my_x;
  1512. X                  new_y = my_y;
  1513. X                  screwdriver();
  1514. X                }
  1515. X        else goto retry;
  1516. X        break;
  1517. X    case ctrl('R'):
  1518. X        clearok(curscr,TRUE);
  1519. X        wrefresh(curscr);
  1520. X        goto retry;
  1521. X    default:
  1522. X        bad_move = TRUE;
  1523. X    }
  1524. X    if(bad_move) {
  1525. X        if(running) {
  1526. X            if(first_move) putchar(BEL);
  1527. X            running = FALSE;
  1528. X            adjacent = FALSE;
  1529. X            waiting = FALSE;
  1530. X            first_move = FALSE;
  1531. X        } else {
  1532. X            putchar(BEL);
  1533. X        }
  1534. X        refresh();
  1535. X        count = 0;
  1536. X        goto retry;
  1537. X    }
  1538. X    first_move = FALSE;
  1539. X    if(dots) erase_dots();
  1540. X    mvaddch(my_y,my_x,' ');
  1541. X    my_x = new_x;
  1542. X    my_y = new_y;
  1543. X    move(my_y,my_x);
  1544. X    if(inch() == ROBOT) munch();
  1545. X    if(inch() == FROBOT) munch();
  1546. X    if(dots) put_dots();
  1547. X    mvaddch(my_y,my_x,ME);
  1548. X    refresh();
  1549. X}
  1550. X
  1551. Xread_com()
  1552. X{
  1553. X    static int     com;
  1554. X
  1555. X    if(count == 0) {
  1556. X        if(isdigit(com = readchar())) {
  1557. X            count = com-'0';
  1558. X            while(isdigit(com = readchar()))
  1559. X                count = count*10+com-'0';
  1560. X        }
  1561. X    }
  1562. X    if(count > 0) count--;
  1563. X    return(com);
  1564. X}
  1565. X
  1566. Xdo_move(dir)    /* implement the users move */
  1567. X    char dir;
  1568. X{
  1569. X    register int x, y;
  1570. X    new_x = my_x+xinc(dir);
  1571. X    new_y = my_y+yinc(dir);
  1572. X    if(adjacent && !first_move) {
  1573. X        for(x = -2; x <= 2; x++) {
  1574. X            for(y = -2; y <= 2; y++) {
  1575. X                move(new_y+y ,new_x+x);
  1576. X                switch(inch()) {
  1577. X                case SCRAP:
  1578. X                    if( waiting )
  1579. X                        break;
  1580. X                case ROBOT:
  1581. X                    if(abs(x) < 2 && abs(y) < 2) {
  1582. X                        bad_move = TRUE;
  1583. X                        return;
  1584. X                    }
  1585. X                    else break;
  1586. X                case FROBOT:
  1587. X                    if (waiting &&
  1588. X                        blocked(new_y, new_x, y, x) )
  1589. X                        break;
  1590. X                    bad_move = TRUE;
  1591. X                    return;
  1592. X                }
  1593. X            }
  1594. X        }
  1595. X    }
  1596. X    move(new_y,new_x);
  1597. X    switch(inch()) {
  1598. X    case SCRAP:
  1599. X        if(moveable_heaps && move_heap(dir))
  1600. X            break;
  1601. X    case VERT:
  1602. X    case HORIZ:
  1603. X        bad_move = TRUE;
  1604. X        return;
  1605. X    }
  1606. X}
  1607. X
  1608. Xmove_heap(dir)    /* push a scrap heap */
  1609. Xchar    dir;
  1610. X{
  1611. X    register int    x, y;
  1612. X
  1613. X    x = new_x + xinc(dir);
  1614. X    y = new_y + yinc(dir);
  1615. X    move(y, x);
  1616. X    switch(inch()) {
  1617. X        case VERT:
  1618. X        case HORIZ:
  1619. X        case SCRAP:
  1620. X        case ROBOT:
  1621. X        case FROBOT:
  1622. X            return FALSE;
  1623. X    }
  1624. X    addch(SCRAP);
  1625. X    mvaddch(new_y,new_x,' ');
  1626. X    return TRUE;
  1627. X}
  1628. END_OF_user.c
  1629. if test 3604 -ne `wc -c <user.c`; then
  1630.     echo shar: \"user.c\" unpacked with wrong size!
  1631. fi
  1632. # end of overwriting check
  1633. fi
  1634. echo shar: End of shell archive.
  1635. exit 0
  1636.